﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOL;
using BLL;
using DAL;
using DevComponents.AdvTree;

namespace QLThueMuonXe
{
    public partial class frmXe : UserControl
    {
        FormMain fm = new FormMain();
        FormXe fx = new FormXe();
        FormHangXe fhx = new FormHangXe();
        HamDungChung hdc = new HamDungChung();

        #region Điều khiển đóng/mở form
        private HamDungChung _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public HamDungChung Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    HamDungChung oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(HamDungChung oldValue, HamDungChung newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmXe;
            }
            else
            {
                btndong.Command = null;
            }
        }
        #endregion

        public frmXe()
        {
            InitializeComponent();
            List<ThongTinHangXe> tthx = fhx.LayDSHangXe();
            cobhangxe.DataSource = tthx;
            cobhangxe.DisplayMember = "hangxe";
            cobhangxe.DisplayMember = "hangxe";
        }

        private void frmXe_Load(object sender, EventArgs e)
        {
            xLoad();
        }

        private Node CreateXeRow(string maxe, string hangxe, string tenxe, string bienso)
        {
            Node node = new Node(maxe);
            node.Cells.Add(new Cell(hangxe));
            node.Cells.Add(new Cell(tenxe));
            node.Cells.Add(new Cell(bienso));
            return node;
        }


        public void xLoad()
        {
            List<ThongTinXe> ttx = fm.LayTTXe();
            advTree1.Nodes.Clear();
            foreach (var row in ttx)
            {
                advTree1.Nodes.Add(CreateXeRow(row.max, row.hangx, row.tenx, row.bienso));
            }
            advTree1.SelectedIndex = 0;
        }

        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            try
            {
                Node node = e.Node;
                txtmaxe.Text = node.Cells[0].Text;
                cobhangxe.Text = node.Cells[1].Text;
                txttenxe.Text = node.Cells[2].Text;
                txtbienso.Text = node.Cells[3].Text.Trim();
            }
            catch (Exception) { }
        }

        public void xoatrang()
        {
            txtmaxe.Text = (hdc.NextID(fx.madatXElonnhat(), "XE") == "Lỗi") ? "XE01" : hdc.NextID(fx.madatXElonnhat(), "XE");
            txtbienso.Text = "";
            txttenxe.Text = "";
            cobhangxe.Text = "";
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            if (btnthem.Text == "Thêm Mới")
            {
                btnthem.Text = "Lưu";
                btnsua.Text = "Hủy Bỏ";
                xoatrang();
                this.advTree1.AfterNodeSelect -= new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
            }
            else
            {
                if (txttenxe.Text == "" || cobhangxe.Text == "")
                {
                    MessageBox.Show("Bạn chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                        fx.themX(txtmaxe.Text, cobhangxe.Text, txttenxe.Text, txtbienso.Text);
                        MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnthem.Text = "Thêm Mới";
                        btnsua.Text = "Sửa";
                         xLoad();
                        this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
                    }
                }
            }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (btnsua.Text == "Sửa")
            {
                fx.suaX(txtmaxe.Text.Trim(), cobhangxe.Text, txttenxe.Text, txtbienso.Text);
                MessageBox.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                xLoad();
            }
            else
            {
                btnthem.Text = "Thêm Mới";
                btnsua.Text = "Sửa";
                this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
                advTree1.SelectedIndex = advTree1.SelectedIndex + 1;

            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("Bạn muốn xóa  xe "+txttenxe.Text+" này ? các thông tin liên quan đến xe này sẽ bị xóa.", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                fx.XoaX(txtmaxe.Text.Trim());
                xLoad();
            }
        }
    }
}
